package cn.zlp.org.controller;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.util.AjaxResult;
import cn.zlp.basic.util.PageList;
import cn.zlp.org.domain.Shop;
import cn.zlp.org.dto.ShopPetDto;
import cn.zlp.org.query.ShopQuery;
import cn.zlp.org.service.IShopService;
import cn.zlp.pet.domain.Pet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺控制接口
 * @author Mr.Zhang
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /** 查询所有
     * 定义一个方法：返回值：List<Shop> +  方法名：findAll + 参数列表：无
     * 处理请求：Get http://localhost:8080/shop
     * GetMapping等价于RequestMapping(value = "",method = RequestMethod.GET)
     */
    @GetMapping
    @ApiOperation(value = "查询所有接口",notes = "")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /** 查询单个
     * 以前处理请求：Get http://localhost:8080/shop?id=1
     * 现在处理请求：Get http://localhost:8080/shop/1
     * GetMapping("/{id}")用{}区分是请求参数，没有用{}是请求地址
     * PathVariable用来接收传过来的参数id
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口",notes = "需要传入参数id")
    public Shop findOne(@PathVariable("id")Long id){
        return shopService.findById(id);
    }

    /** 分页查询 */
    @PostMapping
    @ApiOperation(value = "分页查询接口",notes = "需要传入分页参数")
    public PageList<Shop> pageList(@RequestBody ShopQuery shopQuery){
        PageList<Shop> list = shopService.pageQuery(shopQuery);
        return list;
    }

    /** 店铺入驻 */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻接口",notes = "需要传入入驻信息")
    public AjaxResult register(@RequestBody Shop shop){
        try {
            shopService.register(shop);
            return new AjaxResult();
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("注册失败" + b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误,注册失败" + e.getMessage());
        }
    }

    /** 删除 */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传入参数id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("删除失败");
        }
    }

    /** 批量删除 */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传入参数id集合")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            shopService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /** 修改 */
    @PutMapping
    @ApiOperation(value = "修改接口",notes = "需要传入店铺参数")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            // 调用update方法
            shopService.update(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }
    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/pet + json参数petQuery
    @ApiOperation(value = "分页查询店铺所有宠物信息",notes = "需要传递当前页和每页显示的条数和店铺id")
    @PostMapping("/all/shopPet")
    public PageList<Pet> findShopPetPage(@RequestBody ShopPetDto shopPetDto){
        return shopService.findShopPetPage(shopPetDto);
    }

}
